package com.noahplat.alarm.controller;


import com.noahplat.alarm.pojo.Alarm;
import com.noahplat.alarm.service.AlarmService;
import com.noahplat.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * 作者：宋彦磊
 * 描述：报警信息controller
 * 时间：2021-03-15
 */
@RequestMapping("/api/alarm")
@RestController
@Validated
public class AlarmController {

    @Autowired
    private AlarmService alarmService;

    /**
     * 登记报警信息
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-alarm:AlarmController:addAlarm')")
    @PostMapping("/addAlarm")
    public void addAlarm(@RequestBody Alarm alarm){
        alarmService.addAlarm(alarm);
    }

    /**
     * 根据uid查询报警信息
     * @param page
     * @param limit
     * @param uid
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-alarm:AlarmController:getAlarm')")
    @GetMapping("/getAlarm")
    public Result getAlarm(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer limit,@NotNull(message = "用户ID不能为空 ") @Min(value = 1, message = "用户ID不合法")Long uid){
        return alarmService.getAlarm(page, limit, uid);
    }

    /**
     * 条件查询报警信息接口
     * @param page
     * @param limit
     * @param state
     * @param startdate
     * @param enddate
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-alarm:AlarmController:getAlarms')")
    @GetMapping("/getAlarms")
    public Result getAlarms(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer limit,@NotNull(message = "账套ID不能为空 ") @Min(value = 1, message = "账套ID不合法")Long acctid,String state,String startdate,String enddate){
        return alarmService.getAlarms(page,limit,acctid,state,startdate,enddate);
    }

    /**
     * 根据id查询报警信息
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-alarm:AlarmController:getAlarmById')")
    @GetMapping("/getAlarmById")
    public Result getAlarmById(@NotNull(message = "报警ID不能为空 ") @Min(value = 1, message = "报警ID不合法")Long id){
        return alarmService.getAlarmById(id);
    }

    /**
     * 修改报警信息
     * @param alarm
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-alarm:AlarmController:updateAlarm')")
    @PutMapping("/updateAlarm")
    public Result updateAlarm(@RequestBody Alarm alarm){
        return alarmService.updateAlarm(alarm);
    }
}
